package com.cs.controller;


import com.cs.dto.EvaluateDto;
import com.cs.dto.MalfunctionDto;
import com.cs.dto.Result;
import com.cs.dto.UsersDto;
import com.cs.entity.Evaluate;
import com.cs.service.EvaluateService;
import com.cs.service.MalfunctionService;
import com.cs.service.UsersService;
import com.cs.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 评估信息 前端控制器
 * </p>
 *
 * @author qf
 * @since 2021-08-04
 */
@RestController
@RequestMapping("/api/evaluate")
public class EvaluateController {

    @Autowired
    private MalfunctionService malfunctionService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private EvaluateService evaluateService;

    @GetMapping("specList")
    public Result<MalfunctionDto> getSpecListById(Long modelId) {
        List<MalfunctionDto> list = malfunctionService.getSpecListById(modelId);
        return ResultUtils.returnDataSuccess(list);
    }

    @PostMapping("assess")
    public Result<Evaluate> assess(Long modelId, String optionIds, HttpServletRequest request) {
        // 1.先获取token
        String token = request.getHeader("token");
        // 2.通过 token 获取用户信息
        UsersDto user = usersService.getLoginByToken(token);

        // 3.调用评估方法，进行评估
        Evaluate evaluate = evaluateService.assess(modelId, optionIds, user.getId());
        return ResultUtils.returnDataSuccess(evaluate);
    }

    @GetMapping("{id}/assessInfo")
    public Result<EvaluateDto> assessInfo(@PathVariable Long id) {
        EvaluateDto evaluateDto = evaluateService.assessInfo(id);
        return ResultUtils.returnDataSuccess(evaluateDto);
    }
}

